const mongodb = require("mongodb");
const MongoClient = mongodb.MongoClient;
const url = "mongodb://localhost:27017";
const objectId = mongodb.ObjectId;

function _content(call) {
    MongoClient.connect(url, {
        useUnifiedTopology: true
    }, (err, db) => {
        if (err)
            throw err;
        call(db);
    })
}
//获取id对象
module.exports.objectId = objectId;
//插入数据
module.exports.insert = function (dbName, colName, call, obj) {
    _content((db) => {
        let dbc = db.db(dbName);
        if (!(obj instanceof Array)) {
            obj = [obj];
        }
        dbc.collection(colName).insertMany(obj, function (err, result) {
            if (err)
                throw err;
            if (call instanceof Function) {
                call(result);
            }
            db.close();
        });
    })
}
//查询数据
module.exports.find = function (dbName, colName, call, where = {}, sort = {}, skip = 0, limit = 0) {
    _content((db) => {
        let dbc = db.db(dbName);
        if (typeof where == "number") {
            if (typeof sort == "number") {
                if (arguments.length == 5) {
                    [skip, limit] = [where, sort];
                } else if (arguments.length == 6) {
                    [skip, limit] = [sort, skip];
                }
                sort = {};
            } else {
                if (arguments.length == 6) {
                    limit = where;
                }
            }
            where = {};
        } else if (typeof sort == "number") {
            if (arguments.length == 5) {
                limit = sort;
            } else if (arguments.length == 6) {
                limit = skip;
                skip = sort;
            }
            sort = {};
        }
        dbc.collection(colName).find(where).sort(sort).skip(skip).limit(limit).toArray(function (err, result) {
            if (err)
                throw err;
            if (call instanceof Function) {
                call(result);
            }
            db.close();
        });
    })
}
//删除数据
module.exports.delete = function (dbName, colName, call, where = {}) {
    _content((db) => {
        let dbc = db.db(dbName);
        dbc.collection(colName).deleteOne(where, function (err, result) {
            if (err)
                throw err;
            if (call instanceof Function) {
                call(result);
            }
            db.close();
        });
    })
}
//更新数据
module.exports.update = function (dbName, colName, call, where = {}, data = {}) {
    _content((db) => {
        let dbc = db.db(dbName);
        dbc.collection(colName).updateOne(where, data, function (err, result) {
            if (err)
                throw err;
            if (call instanceof Function) {
                call(result);
            }
            db.close();
        });
    })
}
//更新数据 - 数值增减
module.exports.updateNumber = function (dbName, colName, call, where = {}, data = {}) {
    this.update(dbName, colName, call, where, {
        $inc: data
    })
}
//......扩展更新单独
